<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form"  lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">话题类型</option>
                            {volist name="data" id="vo"}
                            <option value="{$vo.id}">{$vo.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="status" id="status">
                            <option value="">话题状态</option>
                            <option value="1">正常</option>
                            <option value="2">关闭</option>
                            <option value="3">删除</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="top">
                            <option value="">是否置顶</option>
                            <option value="1">否</option>
                            <option value="2">是</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="tj">
                            <option value="">是否推荐</option>
                            <option value="1">否</option>
                            <option value="2">是</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="date" placeholder="发布时间" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="关键字" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>

<script type="text/html" id="table-top">
    <input type="checkbox" name="top" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-top" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.top == 2 ? 'checked' : '' }}>
</script>
<script type="text/html" id="table-tj">
    <input type="checkbox" name="tj" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-tj" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.tj == 2 ? 'checked' : '' }}>
</script>
<script type="text/html" id="table-lock">
    <input type="checkbox" name="lock" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-lock" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.lock == 2 ? 'checked' : '' }}>
</script>

<script type="text/html" id="table-status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-status" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == 2 ? 'checked' : '' }}>
</script>

<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let laydate = layui.laydate;
        let dropdown = layui.dropdown;
        let $ = layui.jquery;
        let common = layui.common;

        laydate.render({
            elem: '#date'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
            ,type: 'date'
        });

        let cols = [
            [{
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '置顶',
                    field: 'top',
                    align: 'center',
                    templet:'#table-top',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '推荐',
                    field: 'tj',
                    align: 'center',
                    templet:'#table-tj',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '锁定',
                    field: 'lock',
                    align: 'center',
                    templet:'#table-lock',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '关闭',
                    field: 'status',
                    align: 'center',
                    templet:'#table-status',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '话题分类',
                    field: 'type_name',
                    align: 'center',
                    width:'20%'
                },
                {
                    title: '帖子内容',
                    field: 'content',
                    align: 'center',
                    width:'20%'
                },
                {
                    title: '发帖人',
                    field: 'username',
                    align: 'center',
                    width:'20%'
                },
                {
                    title: '部门',
                    field: 'department_name',
                    align: 'center',
                    width:'20%'
                },
                {
                    title: '转发数',
                    field: 'forward',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '收藏数',
                    field: 'coll',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '热度',
                    field: 'hot',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '浏览量',
                    field: 'hits',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '回复数',
                    field: 'huifu',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '点赞数',
                    field: 'zan',
                    align: 'center',
                    sort: true,
                    width:'10%'
                },
                {
                    title: '发布时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time',
                    width:'20%'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: '5%',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Topic/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {
                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: [{
                            title: '详情',
                            id: res.data[i].id,
                            type: 1,
                        }, {
                            title: '删除',
                            id: res.data[i].id,
                            type: 2
                        }, {
                            title: '小程序码',
                            id: res.data[i].id,
                            type: 3
                        }],
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {
                                parent.layui.admin.addTab('11'+obj.id, "帖子详情", '/Topic/view?id='+obj.id)
                            } else if (obj.type == 2) {
                                layer.confirm('确定要删除该条数据?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    let loading = layer.load();
                                    $.ajax({
                                        url: "/Topic/delete",
                                        data:{id:obj.id},
                                        dataType: 'json',
                                        type: 'post',
                                        success: function(result) {
                                            layer.close(loading);
                                            if (result.status == 0) {
                                                layer.msg(result.msg, {
                                                    icon: 1,
                                                    time: 1000
                                                }, function() {
                                                    layui.table.reload("user-table");
                                                });
                                            } else {
                                                layer.msg(result.msg, {
                                                    icon: 2,
                                                    time: 1000
                                                });
                                            }
                                        }
                                    })
                                });
                            } else if(obj.type == 3) {
                                let loading = layer.load();
                                $.getJSON('/Topic/getQrCode?id='+obj.id, function(json){
                                    layer.close(loading);
                                    layer.open({
                                        type: 1,
                                        title: '小程序码',
                                        shade: 0.1,
                                        area: [common.isModile()?'100%':'320px', '370px'],
                                        content: '<img src="'+json.data.url+'" alt="">'
                                    });
                                });
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });


        //监听排序事件
        table.on('sort(user-table)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var field = form.val('reload');
            table.reload('user-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                    ,type: field.type
                    ,status: field.status
                    ,top: field.top
                    ,tj: field.tj
                    ,time: field.time
                    ,keys: field.keys
                }
            });
        });

        //监听锁定操作
        form.on('switch(table-lock)', function(obj) {
            $.post("{:url('lock')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });

        //监听置顶操作
        form.on('switch(table-top)', function(obj) {
            $.post("{:url('top')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });

        //监听推荐操作
        form.on('switch(table-tj)', function(obj) {
            $.post("{:url('recommend')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });

        //监听关闭操作
        form.on('switch(table-status)', function(obj) {
            $.post("{:url('toggle')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'export') {
                window.export();
            }
        });


        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出员工数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?time="+field.time+"&keys="+field.keys+"&status="+field.status+"&type="+field.type+"&top="+field.top+"&tj="+field.tj, "_blank");
            });
        };

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
